﻿using System;
using System.Collections.Generic;
using Yeap.Config;
using Yeap.Kernel.Security;

namespace Yeap.Kernel.Storage
{
    public partial class User: IUser
    {
        UserSettingRepository userSettingRepository = new UserSettingRepository();

        #region Авторизация

        public virtual void SetPassword(String psw)
        {
            if (String.IsNullOrWhiteSpace(psw))
            {
                return;
            }

            if (psw != Password)
            {
                Salt = HashEngine.GenerateSalt();
                Password = HashEngine.GenerateHash(psw, Salt);
                PasswordModificationDate = DateTime.Now;
            }
        }

        public virtual bool IsEqualPassword(String psw)
        {
            if (String.IsNullOrWhiteSpace(psw))
            {
                return false;
            }

            psw = HashEngine.GenerateHash(psw, Salt);

            return Password == psw;
        }

        public bool IsLogged { get; set; }

        #endregion

        #region Приведение к типам пользователей

        public Student GetStudentForm()
        {
            if (this.UserType != (int?) UserTypes.Student)
            {
                return null;
            }

            if (!this.Course.HasValue)
            {
                throw new Exception(Constants.ExceptionMessages.CourseIsNullException);
            }

            return new Student
                                      {
                                          Id = this.Id,
                                          Login = this.Login,
                                          FirstName = this.FirstName,
                                          LastName = this.LastName,
                                          Course = this.Course.Value,
                                          Properties = this.Properties,
                                      };
        }

        public Professor GetProfessorForm()
        {
            if (this.UserType != (int?)UserTypes.Professor)
            {
                return null;
            }

            if (String.IsNullOrWhiteSpace(this.Rank) )
            {
                throw new Exception(Constants.ExceptionMessages.RankIsNullException);
            }

            return new Professor()
            {
                Id = this.Id,
                Login = this.Login,
                FirstName = this.FirstName,
                LastName = this.LastName,
                Rank = this.Rank,
                Properties = this.Properties,
            };
        }

        #endregion

        #region Дополнительные свойства

        private bool inititalisedProperties = false;
        private Dictionary<String, String> properties = null;

        public String FullName
        {
            get { return LastName + " " + FirstName; }
        }

        public Dictionary<String, String> Properties
        {
            get
            {
                if (!inititalisedProperties)
                {
                    var userSettingsList = userSettingRepository.GetSettingsByUserId(Id);
                    if (properties == null)
                    {
                        properties = new Dictionary<string, string>();
                    }
                    properties.Clear();
                    foreach (var userSetting in userSettingsList)
                    {
                        properties.Add(userSetting.PropertyName, userSetting.PropertyValue);
                    }
                    inititalisedProperties = true;
                }
                return properties;
            }
        }


        #endregion

    }

}